home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / PROGRAMMING / DESKLIBC / SOURCES.ZIP / DeskLib / !DLSources / Libraries / Window / c / ModeChange < prev    next >
Text File  |  1995-09-02  |  5KB  |  150 lines

  1. /*
  2.     ####             #    #     # #
  3.     #   #            #    #       #          The FreeWare C library for 
  4.     #   #  ##   ###  #  # #     # ###             RISC OS machines
  5.     #   # #  # #     # #  #     # #  #   ___________________________________
  6.     #   # ####  ###  ##   #     # #  #                                      
  7.     #   # #        # # #  #     # #  #    Please refer to the accompanying
  8.     ####   ### ####  #  # ##### # ###    documentation for conditions of use
  9.     ________________________________________________________________________
  10.  
  11.     File:    Window.ModeChange.c
  12.     Author:  Copyright © 1993 Tim Browse and Jason Williams
  13.              (Original code by Tim, massive changes and rewriting by Jason ;-)
  14.     Version: 1.02 (28 Jun 1993)
  15.     Purpose: High-level window management functions: 
  16.              Fix windows to use new font handles after a mode change.
  17.              (This even works with open windows, so long as they were created
  18.              with Window_ calls)
  19. */
  20.  
  21.  
  22. #include "Desklib:Error.h"
  23. #include "Desklib:Event.h"
  24. #include "Desklib:Font.h"
  25. #include "Desklib:LinkList.h"
  26. #include "Desklib:Screen.h"
  27. #include "Desklib:Template.h"
  28. #include "Desklib:Window.h"
  29. #include "Desklib:WimpSWIs.h"
  30.  
  31. #include "WindowDefs.h"
  32.  
  33. #include <string.h>
  34.  
  35.  
  36. extern linklist_header window_listanchor;
  37.  
  38.  
  39. /* This constant is used to mask out all icon flag fields except the font handle */
  40. #define FONT_MASK ((unsigned) 0xFF000000)
  41.  
  42.  
  43. static void MapFontHandles(window_block  *wblock,
  44.                            window_handle window,
  45.                            font_array    *fontarray)
  46. /*  This function goes through a window block, and changes each icon's font
  47.  *  handle (if any) by looking up its current handle in the given fontarray,
  48.  *  and using that entry in the fontarray as the new font handle to use
  49.  */
  50. {
  51.   icon_block    *icons;
  52.   int           i;
  53.  
  54.   icons = (icon_block *) (wblock + 1);    /* pointer arithmetic! */
  55.   for (i = 0; i < wblock->numicons; i++)
  56.   {
  57.     if (icons[i].flags.data.font)
  58.     {
  59.       if (window != 0)    /* The window is open - change all on-screen icons */
  60.       {
  61.         icon_flags flags;
  62.         flags.font.handle = fontarray->fonts[icons[i].flags.font.handle];
  63.         Wimp_SetIconState(window, i, flags.value & FONT_MASK, FONT_MASK);
  64.       }
  65.  
  66.       /* Change icon font handle in the window defn block */
  67.       icons[i].flags.font.handle =
  68.                           fontarray->fonts[icons[i].flags.font.handle];
  69.     }
  70.   }
  71. /* cc warns 'flags' may be used before being set    */
  72. }
  73.  
  74.  
  75.  
  76. extern void Window_ModeChange(void)
  77. {
  78.   windowrec       *record;
  79.   template_record *tptr;
  80.   font_defn       defn;
  81.   font_array      fontconvert;
  82.   int             i;
  83.  
  84.   if ((int)template_fontarray < 0)
  85.     return;                       /* You aren't using outline fonts, stupid! */
  86.  
  87.   /*  Go through the template's font usage array...
  88.    *  For each font we are using, re-find it at the same point size to get
  89.    *  the font system to cache the font for the current screen mode.
  90.    *  We fill in a conversion table which maps each old font handle to each
  91.    *  new one (fontconvert.fonts[old_handle] = new_handle)
  92.    */
  93.  
  94.   for (i = 0; i < 256; i++)
  95.   {
  96.     fontconvert.fonts[i] = 0;
  97.     if (template_fontarray->fonts[i] > 0)       /* If we are using this font */
  98.     {
  99.       font_handle h = 0;
  100.  
  101.       Font_ReadDefn(i, &defn);             /* Find out its name and size ... */
  102.                                            /* ...and get a new handle for it */
  103.       Font_FindFont(&h, defn.name, defn.xsize, defn.ysize, 0, 0);
  104.       fontconvert.fonts[i] = h;  /* (BTW, it's OK if the handle is the same) */
  105.     }
  106.   }
  107.  
  108.   /*  Replace all of the fonts we are using with the new handles
  109.    *  Firstly, go through all of the Window_Create'd windows (open windows)
  110.    *  and fix them... (Note that if any windows use fonts but were not
  111.    *  Window_Create'd then
  112.    *    a) they won't be fixed, and
  113.    *    b) the font usage is probably now 0, so if the cache fills up,
  114.    *       the font may be lost/replaced, and NASTY things will happen!
  115.    */
  116.  
  117.   record = LinkList_FirstItem(&window_listanchor);
  118.   while (record != NULL)
  119.   {
  120.     MapFontHandles((window_block *) record->memory,
  121.                    record->window, &fontconvert);
  122.  
  123.     record = LinkList_NextItem(&record->header);
  124.   }
  125.  
  126.   /*  Now do the template definitions themselves (otherwise the next time
  127.    *  you open a window all the font handles will be wrong and won't you
  128.    *  look silly?)
  129.    */
  130.   tptr = (template_record *) template_list.next;
  131.   while (tptr != NULL)
  132.   {
  133.     MapFontHandles(tptr->windowdef, (window_handle) 0, &fontconvert);
  134.     tptr = (template_record *) tptr->header.next;
  135.   }
  136.  
  137.   /*  Lose all the old fonts and install new font list into Template module
  138.    *  so that we know which fonts to lose next time, or when we quit.
  139.    *  We Font_FindFont'd each font in the conversion table
  140.    *  Thus, for each font handle in the conversion table, we increment its
  141.    *  usage count. (Note that Font_LoseAllFonts() clears the table out to 0's)
  142.    */
  143.  
  144.   Font_LoseAllFonts(template_fontarray);
  145.  
  146.   for (i = 0; i < 256; i++)
  147.     if (fontconvert.fonts[i] != 0)
  148.       template_fontarray->fonts[fontconvert.fonts[i]]++;
  149. }
  150.